/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.util;

//import java.security.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.Timestamp;

/**
 *
 * @author Wei Liu
 */
public class StringHelper {
    
    public static String setFirstCharUpperCase(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    public static boolean validateString(String str)
    {

        boolean breturn = true;
        char[] nameArray = str.toCharArray();
        for (char ch : nameArray)
        {
            if (ch <= 'z' && ch >= 'a')
            {
            } 
            else if (ch <= 'Z' && ch >= 'A')
            {
            }
            else if(ch == ' ')
            {
            }
            else
            {
                breturn = false;
            }
        }
        return breturn;
    }

    public static String getDate(String dateTime)
    {
        return dateTime.substring(0, 10);
    }

    public static String getTime(String dateTime)
    {
        return dateTime.substring(11);
    }

    public static String CharArrayToString(char[] array)
    {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < array.length; i++)
        {
            sb.append(array[i]);
        }

        return sb.toString();
    }

    //To validate date format yyyy-MM-dd
    public static boolean isValidDate(String date)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date testDate = null;
        try
        {
            testDate = sdf.parse(date);
        }
        catch (ParseException e)
        {
            return false;
        }
        if (!sdf.format(testDate).equals(date))
        {
            return false;
        }

        return true;

    }

    //To validate time format HH:mm:ss
    public static boolean isValidTime(String time)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

        Date testDate = null;
        try
        {
            testDate = sdf.parse(time);
        }
        catch (ParseException e)
        {
            return false;
        }
        if (!sdf.format(testDate).equals(time))
        {
            return false;
        }

        return true;

    }

    //To convert string to time stamp
    public static Timestamp strTotime(String str) throws ParseException
    {
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date date;

       date = sdf.parse(str);
       
       Timestamp timestamp = new Timestamp(date.getTime());
       return timestamp;
    }

    public static Timestamp strTonewtime(String str) throws ParseException
    {
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       Date date;

       date = sdf.parse(str);

       Timestamp timestamp = new Timestamp(date.getTime());
     
       return timestamp;
    }

    public static Timestamp strTonewdate(String str) throws ParseException
    {
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
       Date date;

       date = sdf.parse(str);

       Timestamp timestamp = new Timestamp(date.getTime());

       return timestamp;
    }
     //To validate if the input to number by Jenny APR/08/2010

    public static boolean isValidNumber(String totalNumber)
    {
        try
        {
              int  temp =  Integer.parseInt(totalNumber);
        }
        catch (NumberFormatException e)
        {
              return false ;
        }
         return true ;

    }

    public static boolean before(Timestamp frm, Timestamp to)
    {
        if(to.after(frm))
            return true;
        else if(to.equals(frm))
            return true;
        else
            return false;
    }

    public static boolean timerange(Timestamp frm, Timestamp to, Timestamp test, Timestamp test2)
    {
        if((test.after(frm) || test.equals(frm)) && (test.before(to) || test.equals(to)))
            return true;
        else if(test.before(frm) && test2.after(frm))
            return true;
        else 
            return false;
    }

    public static String formatDouble(double number)
    {
        return String.format("%.2f", number);
    }

    public static String formatPrice(double price)
    {
        return "$" + String.format("%.2f", price);
    }

    public static String hideCardNumber(String cardNumber)
    {
        return cardNumber.substring(0, 4) + "********" + cardNumber.substring(12,16);
    }


    public static boolean limit(String s)
    {
        if(s.length() > 45)
            return true;
        else
            return false;
    }

    public static boolean limitone(String s)
    {
        if(s.length() > 10)
            return true;
        else
            return false;
    }
}
